﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SuperGo.Silverlight {
	public abstract class ControlPageBase : ControlBase {
		protected const string _windowShowAnimationName = "AnimateShow";
		protected const string _windowHideAnimationName = "AnimateHide";

		public ControlPageBase() {
			this.Loaded += new RoutedEventHandler(ControlPageBase_Loaded);
		}

		//protected void SetWidths(double padding, params FrameworkElement[] elements) {
		//    if (elements.Length < 1)
		//        return;

		//    int visibleElementCount = elements.Where(e => e.IsVisible()).Count();
		//    FrameworkElement parent = elements.First().Parent as FrameworkElement;
		//    if (parent == null)
		//        throw new Exception("The parent of the controls must be a FrameworkElement.");

		//    double parentWidth = parent.ActualWidth - (padding * visibleElementCount);
		//    double childWidth = parentWidth / visibleElementCount;

		//    foreach (FrameworkElement element in elements) {
		//        if (element.IsVisible()) {
		//            element.Width = childWidth;
		//        }
		//    }
		//}

		protected Panel _rootElement;

		/// <summary>
		/// Set in the derived class after it loads the xaml file
		/// This is the root element from which FindByName works
		/// </summary>
		protected Panel RootElement {
			get { return _rootElement; }
			set { _rootElement = value; }
		}

		/// <summary>
		/// Simplifies calls to FindName by selecting the correct root
		/// element from which to base the search and also by returning
		/// back a strongly-typed reference to the element
		/// a common stumbling block in SL code is to call Control.FindName
		/// which doesn't return what you want (at leat in 1.1a). Instead, 
		/// you need to call FindName from the root element.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="name"></param>
		/// <returns></returns>
		protected T FindByName<T>(string name) where T : DependencyObject {
			if (_rootElement == null)
				return null;
			return _rootElement.FindName(name) as T;
		}

		private void ControlPageBase_Loaded(object sender, RoutedEventArgs e) {
			Panel layoutRoot = this.FindName("LayoutRoot") as Panel;
			if (layoutRoot == null) {
				throw new Exception("LayoutRoot not implemented");
			}
			_rootElement = layoutRoot;
		}

        public virtual void Show() {
            this.Visibility = Visibility.Visible;

			if (FindByName<Storyboard>(_windowShowAnimationName) == null) {
				this.Opacity = 1;
			} else {
				FindByName<Storyboard>(_windowShowAnimationName).Begin();
			}
        }

		public virtual void Hide() {
			if (FindByName<Storyboard>(_windowHideAnimationName) == null) {
				this.Visibility = Visibility.Collapsed;
			} else {
				FindByName<Storyboard>(_windowHideAnimationName).Begin();
			}
		}

	}
}
